package com.movisol.messenger.views.controls;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.movisol.messenger.R;
import com.movisol.messengercommunicator.data.Conversation;

public class ContactConversationRow extends LinearLayout {
	
	private TextView nickNameTv;
	private TextView optionalTv;
	private final List<String> nickNameList;
	private String lastMessage;
	private Conversation conversation;
	private final Context context;
	private Button profileButton;
	private ImageView imageContactRow;
	
	public ContactConversationRow(Context context, Conversation conversation, List<String> nickNameList) {
		super(context);
		this.context = context;
		
		String infService = Context.LAYOUT_INFLATER_SERVICE;
		LayoutInflater li = (LayoutInflater)this.context.getSystemService(infService);
		li.inflate(R.layout.contactrow, this, true);
		
		this.conversation = conversation;
		this.nickNameList = nickNameList;
		this.lastMessage = conversation.getLastMessage();
		
		initializeControls();
		
	}

	private void initializeControls() {
		
		nickNameTv = (TextView) findViewById(R.id.tvNickNameContactRow);
		optionalTv = (TextView) findViewById(R.id.tvOptionalContactRow);
		profileButton = (Button) findViewById(R.id.imBtnContactRow);
		imageContactRow = (ImageView) findViewById(R.id.imageContactRow);
		
		profileButton.setVisibility(View.GONE);
		
		String nickNames = "";
		
		for(String nickname : nickNameList)
			nickNames += nickname+",";
		
		nickNames = nickNames.substring(0, nickNames.length()-1);
		nickNameTv.setText(nickNames);
		
		if(nickNameList.size() > 1)
			imageContactRow.setImageDrawable(getResources().getDrawable(R.drawable.img_unknown_group));
		if (lastMessage != null) {
			lastMessage = lastMessage.replace("\n", " ");
		
			if (lastMessage.length() > 22) {
				lastMessage = lastMessage.substring(0, 22).concat(" ...");
			}
		}
		
		optionalTv.setText(lastMessage);

	}


	@Override
	public void setSelected(boolean selected) {
		super.setSelected(selected);

		if(selected)
		{
			Drawable d = new ColorDrawable(Color.GRAY);
			d.setAlpha(128);
			setBackgroundDrawable(d);
		}
		else
		{
			setBackgroundDrawable(null);
		}
	}

	public Conversation getConversation() {
		return conversation;
	}

	public void setConversation(Conversation conversation) {
		this.conversation = conversation;
	}
	
	
	 

}
